/*
 * @Author: your name
 * @Date: 2020-05-12 14:32:44
 * @LastEditTime: 2020-06-03 21:23:10
 * @LastEditors: xgq
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\models\user\m_user.js
 */ 
const Sequelize = require('sequelize')

module.exports = (sequelize, DataTypes ) => {

  class User extends Sequelize.Model {
    // 更改用户信息
    static async updateUser(update,where){
      var result=await this.update(update,where);
      return result;
    }
    // 获取用户详情
    static findOne_byid(id){
      let condition = {
        attributes: ['id',  'nickname', 'mobile','sex', 'email', 'avatar', 'about_me', 'last_seen', 'verified', 'register_ip', 'register_date', 'score', 'praises', 'praises_hit', 'downloads', 'wx_openid', 'wx_unionid', 'show_tips','country','company','occupation','province','city','area','base_user_id'],
        where: { "id": id }
      }
      return this.findOne(condition);
    }
    // 获取用户列表
    static async getUserList(pars){
      var [page = 1, pagesize = 12, order = null, where = null] = [pars.page, pars.pagesize, pars.order, pars.where]
      let condition = {
        attributes: ['id',  'nickname', 'sex', 'email', 'avatar', 'about_me', 'last_seen', 'verified', 'register_ip', 'register_date', 'score', 'praises', 'praises_hit', 'downloads', 'wx_openid', 'wx_unionid', 'show_tips','country','company','occupation','province','city','area','base_user_id'],
        offset: pagesize * (page - 1),
        raw: true,
        limit: parseInt(pagesize)
      }
      if (where !== null) condition.where = where
      if (order !== null) {
        condition.order = order
      }
      return this.findAndCountAll(condition);
    }

    // 删除收藏
    async deleteCollect(pars) {
      let result = await this.destroy({
        'where': {
          'img_id': pars.image_id,
          'user_id': pars.uid,
          'operate': 2
        }
      })
      return result;
    }

    // 添加收藏
    async pushCollect(pars) {
      let result = await this.create({
        img_id: pars.image_id,
        user_id: pars.uid,
        operate: 2
      })
      return result;
    }
    // 获取某一图片的收藏数量
    async getCollectCount(id){
      let result = await this.count({
        where:{
          img_id:id,
          operate:2
        }
      })
      return result;
    }

    static find_wx_openid(openid, unionid){
      let condition = {
          attributes: ['id', 'nickname','avatar','base_user_id'],
          where: { "wx_openid": openid, "wx_unionid": unionid }
      }
      return this.findOne(condition);
    }

    static find_byid(id){
      let condition = {
        attributes: ['id', 'nickname','avatar','wx_unionid','wx_openid'],
        where: { "id": id }
      }
      return this.findOne(condition);
    }
  }
  
  User.init({
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    nickname: { type: DataTypes.STRING(64) },
    avatar: { type: DataTypes.STRING(64) },
    sex: { type: DataTypes.STRING(1) },
    mobile:{ type: DataTypes.STRING(12), comment:'手机号' },
    score: { type: DataTypes.INTEGER },
    comments:{ type: DataTypes.INTEGER, comment:'用户评论数'},
    email:{ type: DataTypes.STRING(64), comment:'邮箱地址' },
    last_seen:{ type: DataTypes.INTEGER, comment:'最后登录时间戳' },
    register_ip:{ type: DataTypes.STRING(20), comment:'注册IP' },
    register_date:{ type: DataTypes.INTEGER, comment:'注册时间戳' },
    praises:{ type: DataTypes.INTEGER, comment:'被赞次数'},
    praises_hit:{ type: DataTypes.INTEGER, comment:'点赞次数'},
    downloads:{ type: DataTypes.INTEGER, comment:'下载次数'},
    wx_openid:{ type: DataTypes.STRING(45), comment:'微信OPENID' },
    wx_unionid:{ type: DataTypes.STRING(45), comment:'微信unionid' },
    country:{ type: DataTypes.STRING(45), comment:'国家' },
    province:{ type: DataTypes.STRING(45), comment:'省份' },
    city:{ type: DataTypes.STRING(45), comment:'城市' },
    area:{ type: DataTypes.STRING(45), comment:'区域' },
    company:{ type: DataTypes.STRING(45), comment:'公司名称' },
    occupation:{ type: DataTypes.STRING(45), comment:'职业' },    
    base_user_id:{ type: DataTypes.INTEGER, comment:'用户OSS单点登录系统对应ID' },
    base_reg_from :{ type: DataTypes.STRING(10), comment:'单点登录其它站点来源' },
  }, {
    sequelize,
    modelName: 'user',
    freezeTableName: true,
    timestamps: false,
    comment: "用户表"
  });

  return User;
}